﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class DisciplinesController : Controller
    {
        private atletisEntities db = new atletisEntities();

        //
        // GET: /Disciplines/

        public ViewResult Index()
        {
            var disciplines = db.Disciplines.Include("DisciplinesGroups").ToList();
            ViewBag.DisciplinesGroups = new SelectList(db.DisciplinesGroups.Where(dl => dl.Active == true).OrderBy(dl => dl.Name).ToList(), "ID", "Name");
            return View(disciplines);
        }

        //
        // GET: /Disciplines/Details/5

        public ViewResult Details(int id)
        {
            Disciplines disciplines = db.Disciplines.Single(d => d.ID == id);
            return View(disciplines);
        }

        //
        // GET: /Disciplines/Create

        public ActionResult Create()
        {
            ViewBag.DisciplineGroupId = new SelectList(db.DisciplinesGroups, "ID", "Name");
            return View();
        } 

        //
        // POST: /Disciplines/Create

        [HttpPost]
        public ActionResult Create(Disciplines disciplines)
        {
            disciplines.Modified = DateTime.Now;
            disciplines.Sync = true;
            
            if (ModelState.IsValid)
            {
                db.Disciplines.AddObject(disciplines);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.DisciplineGroupId = new SelectList(db.DisciplinesGroups, "ID", "Name", disciplines.DisciplineGroupId);
            return View(disciplines);
        }
        
        //
        // GET: /Disciplines/Edit/5
 
        public ActionResult Edit(int id)
        {
            Disciplines disciplines = db.Disciplines.Single(d => d.ID == id);
            ViewBag.DisciplineGroupId = new SelectList(db.DisciplinesGroups, "ID", "Name", disciplines.DisciplineGroupId);
            return View(disciplines);
        }

        //
        // POST: /Disciplines/Edit/5

        [HttpPost]
        public ActionResult Edit(Disciplines disciplines)
        {
            disciplines.Modified = DateTime.Now;
            disciplines.Sync = true;
            
            if (ModelState.IsValid)
            {
                db.Disciplines.Attach(disciplines);
                db.ObjectStateManager.ChangeObjectState(disciplines, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DisciplineGroupId = new SelectList(db.DisciplinesGroups, "ID", "Name", disciplines.DisciplineGroupId);
            return View(disciplines);
        }

        //
        // GET: /Disciplines/Delete/5
 
        public ActionResult Delete(int id)
        {
            Disciplines disciplines = db.Disciplines.Single(d => d.ID == id);
            return View(disciplines);
        }

        //
        // POST: /Disciplines/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Disciplines disciplines = db.Disciplines.Single(d => d.ID == id);
            db.Disciplines.DeleteObject(disciplines);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}